package android.microblog.Sina;

import java.io.File;
import java.util.List;

import weibo4andriod.Configuration;
import weibo4andriod.WeiboException;
import weibo4andriod.http.HttpClient;
import weibo4andriod.http.PostParameter;
import weibo4andriod.http.Response;

public class SinaUtil {
	public static String CONSUMER_KEY = "956917457";
	public static String CONSUMER_SECRET = "9d3fd5e92b04c3894f2c47507d3797d0";
	private static HttpClient http = new HttpClient();
	private static String baseURL = Configuration.getScheme()
			+ "api.t.sina.com.cn/";
	private static String searchBaseURL = Configuration.getScheme()
			+ "api.t.sina.com.cn/";

	public static Response get(String url, boolean authenticate)
			throws WeiboException {
		return get(url, null, authenticate);
	}

	/**
	 * Issues an HTTP GET request.
	 * 
	 * @param url
	 *            the request url
	 * @param params
	 *            the request parameters
	 * @param authenticate
	 *            if true, the request will be sent with BASIC authentication
	 *            header
	 * @return the response
	 * @throws WeiboException
	 *             when Weibo service or network is unavailable
	 */
	public static Response get(String url, PostParameter[] params,
			boolean authenticate) throws WeiboException {
		if (url.indexOf("?") == -1) {
			url += "?source=" + CONSUMER_KEY;
		} else if (url.indexOf("source") == -1) {
			url += "&source=" + CONSUMER_KEY;
		}
		if (null != params && params.length > 0) {
			url += "&" + HttpClient.encodeParameters(params);
		}
		return http.get(url, authenticate);
	}

	public static void addParameterToList(List<PostParameter> params,
			String paramName, String paramValue) {
		if (null != paramValue) {
			params.add(new PostParameter(paramName, paramValue));
		}
	}

	public static Response post(String url) throws WeiboException {
		return http.post(url);
	}

	public static Response post(String url, boolean authenticated)
			throws WeiboException {
		return http.post(url, authenticated);
	}

	public static Response post(String url, PostParameter[] postParameters)
			throws WeiboException {
		return http.post(url, postParameters);
	}

	public static Response post(String url, PostParameter[] postParameters,
			boolean authenticated) throws WeiboException {
		return http.post(url, postParameters, authenticated);
	}
	public static Response multPartURL(String fileParamName,String url,  PostParameter[] params,File file,boolean authenticated) throws WeiboException{
		return http.multPartURL(fileParamName, url, params, file, authenticated);
	}
	public static String getBaseURL() {
		return baseURL;
	}

	public static String getSearchBaseURL() {
		return searchBaseURL;
	}
}
